//
//  KSChoosePressageView.swift
//  Supermarket
//
//  Created by Black_Crystal on 2017/10/17.
//  Copyright © 2017年 kushao. All rights reserved.
//

import UIKit

protocol KSChoosePressageDelegate: NSObjectProtocol {
    func choosePressage(row: NSInteger)
}

class KSChoosePressageView: UIView {
    
    var pressageArray: [String] = []
    var delegate: KSChoosePressageDelegate?
    private var pickPressageView: UIPickerView = UIPickerView()
    private var selectRow: NSInteger = 0
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.black.withAlphaComponent(0.3)
        
        let backView = UIView()
        backView.backgroundColor = UIColor.white
        self.addSubview(backView)
        backView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(150)
            make.width.equalTo(KSConstants.kScreenWidth/3*2)
            make.height.equalTo(250)
        }
        
        pickPressageView.backgroundColor = UIColor.white
        pickPressageView.delegate = self
        pickPressageView.dataSource = self
        backView.addSubview(pickPressageView)
        pickPressageView.snp.makeConstraints { (make) in
            make.top.left.right.equalTo(0)
            make.bottom.equalTo(-50)
        }
        
        let canaleBtn = UIButton()
        canaleBtn.backgroundColor = KSColor.kColor_9
        canaleBtn.setTitle("取消", for: .normal)
        canaleBtn.setTitleColor(UIColor.white, for: .normal)
        canaleBtn.addTarget(self, action: #selector(cancleEvent(button:)), for: .touchDown)
        backView.addSubview(canaleBtn)
        canaleBtn.snp.makeConstraints { (make) in
            make.left.bottom.equalTo(0)
            make.right.equalTo(backView.snp.centerX)
            make.height.equalTo(50)
        }
        
        let confirBtn = UIButton()
        confirBtn.setTitle("确定", for: .normal)
        confirBtn.setTitleColor(UIColor.black, for: .normal)
        confirBtn.addTarget(self, action: #selector(confirEvent(button:)), for: .touchDown)
        backView.addSubview(confirBtn)
        confirBtn.snp.makeConstraints { (make) in
            make.right.bottom.equalTo(0)
            make.left.equalTo(backView.snp.centerX)
            make.height.equalTo(50)
        }
    }
    
    @objc func cancleEvent(button: UIButton) {
        self.isHidden = true
    }
    
    @objc func confirEvent(button: UIButton) {
        self.delegate?.choosePressage(row: selectRow)
        self.isHidden = true
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension KSChoosePressageView: UIPickerViewDataSource,UIPickerViewDelegate {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pressageArray.count
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return pressageArray[row]
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        self.selectRow = row
    }
}
